package com.desire.controller;

import com.desire.bean.User;
import com.desire.remote.UserRemoteClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author: littleDesire
 * @Date: 2022-06-09 9:53
 * @Version: 1.0
 */
@RestController
public class PlanController {
    @Autowired(required = false)
    UserRemoteClient userRemoteClient;

    /**
     * 普通方式调用
     */
    @GetMapping("/plan/query")
    public Map<String, Object> query() {
        Map<String, Object> map = new HashMap<>();
        map.put("tools", "big");
        map.put("details", userRemoteClient.queryUser());
        return map;
    }

    /**
     * 路径传值方式调用
     */
    @GetMapping("/plan/queryPath")
    public Map<String, Object> queryPath() {
        Map<String, Object> map = new HashMap<>();
        map.put("details", userRemoteClient.queryPath("12341"));
        return map;
    }

    /**
     * 普通带参方式调用
     */
    @GetMapping("/plan/queryParams")
    @HystrixCommand(fallbackMethod = "error")
    public Map<String, Object> queryParams() {
        Map<String, Object> map = new HashMap<>();
        map.put("details", userRemoteClient.queryParams("desire", "123456"));
        return map;
    }
    public Map<String, Object> error() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "挂了挂了挂了");
        return map;
    }

    /**
     * map传值方式调用 get
     */
    @GetMapping("/plan/queryGetMap")
    public void queryGetMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("id", "001");
        map.put("name", "desire");
        userRemoteClient.queryGetMap(map);
    }

    /**
     * map传值方式调用 post
     */
    @PostMapping("/plan/queryPostMap")
    public void queryPostMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("id", "001");
        map.put("name", "desire");
        userRemoteClient.queryPostMap(map);
    }

    /**
     * 对象传值方式调用
     */
    @PostMapping("/plan/queryPostUser")
    public void queryPostUser() {
        User user = new User();
        user.setAge(18);
        user.setName("desire");
        userRemoteClient.queryPostUser(user);
    }

    /**
     * SpringQueryMap 官方的传值方式 map
     * 接收不到map参数  ，会报错
     */
    @GetMapping("/plan/queryStringQueryMap")
    public void queryStringQueryMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("id", "001");
        map.put("name", "desire");
        userRemoteClient.queryStringQueryMap(map);
    }


    /**
     * SpringQueryMap 官方的传值方式 User
     */
    @GetMapping("/plan/queryStringQueryMapUser")
    public void queryStringQueryMapUser() {
        User user = new User();
        user.setAge(18);
        user.setName("desire");
        userRemoteClient.queryStringQueryMapUser(user);
    }

    // 测试连接
    /*
    GET http://localhost:7003/plan/query
Accept: application/json

###

GET http://localhost:7003/plan/queryPath
Accept: application/json

###

GET http://localhost:7003/plan/queryParams
Accept: application/json

###

GET http://localhost:7003/plan/queryGetMap
Accept: application/json

###

POST http://localhost:7003/plan/queryPostMap
Accept: application/json

###

POST http://localhost:7003/plan/queryPostUser
Accept: application/json

###

GET http://localhost:7003/plan/queryStringQueryMap
Accept: application/json
# 会报错
###

GET http://localhost:7003/plan/queryStringQueryMapUser
Accept: application/json

###


    */
}
